package com.bdmc.web.controller.base;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.base.domain.bo.BasePersonBo;
import com.bdmc.base.domain.vo.BaseDeviceImportVo;
import com.bdmc.base.domain.vo.BasePersonVo;
import com.bdmc.base.service.IBasePersonService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.excel.ExcelResult;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.system.listener.BasePersonImportListener;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 人员控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/person")
public class BasePersonController extends BaseController {

    private final IBasePersonService iBasePersonService;

    /**
     * 查询人员列表
     */
    @SaCheckPermission("base:person:list")
    @GetMapping("/list")
    public TableDataInfo<BasePersonVo> list(@Validated(QueryGroup.class) BasePersonBo bo, PageQuery pageQuery) {
        return iBasePersonService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出人员列表
     */
    @SaCheckPermission("base:person:export")
    @Log(title = "人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BasePersonBo bo, HttpServletResponse response) {
        List<BasePersonVo> list = iBasePersonService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员", BasePersonVo.class, response);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户工牌信息", BaseDeviceImportVo.class, response);
    }

    @PostMapping("/importData")
    public AjaxResult<Void> importPerson(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BaseDeviceImportVo> result = ExcelUtil.importExcel(file.getInputStream(), BaseDeviceImportVo.class,
                new BasePersonImportListener());
        return AjaxResult.success(result.getAnalysis());
    }

    /**
     * 获取人员详细信息
     */
    @SaCheckPermission("base:person:query")
    @GetMapping("/{personId}")
    public AjaxResult<BasePersonVo> getInfo(@PathVariable("personId") Long personId) {
        return AjaxResult.success(iBasePersonService.queryById(personId));
    }

    /**
     * 新增人员
     */
    @SaCheckPermission("base:person:add")
    @Log(title = "人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(iBasePersonService.insertByBo(bo));
    }

    /**
     * 修改人员
     */
    @SaCheckPermission("base:person:edit")
    @Log(title = "人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(iBasePersonService.updateByBo(bo));
    }

    @SaCheckPermission("base:person:bind")
    @Log(title = "绑定人员与设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("bind")
    public AjaxResult<Void> bind(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(iBasePersonService.bindDevice(bo) );
    }

    @SaCheckPermission("base:person:bind")
    @Log(title = "解绑人员与设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("unbind")
    public AjaxResult<Void> unbind(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(iBasePersonService.unbindDevice(bo) );
    }

    /**
     * 删除人员
     */
    @SaCheckPermission("base:person:remove")
    @Log(title = "人员" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{personIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] personIds) {
        return toAjax(iBasePersonService.deleteWithValidByIds(Arrays.asList(personIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<BasePersonVo>> combo(BasePersonBo bo){
        return AjaxResult.success(iBasePersonService.queryList(bo));
    }
}
